﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChGK.Data;
using System.Web.UI.WebControls;
using CHGK.web.UI.Core;

namespace CHGK.web.UI.Models
{
    public class UserRepositry
    {
            private CHGKModelContainer _CHGKContainer;
            public UserRepositry()
            {
                _CHGKContainer = new CHGKModelContainer();
            }
        
            public UserRepositry(CHGKModelContainer CHGKContainer)
            {
                _CHGKContainer = CHGKContainer;
            }

            public IEnumerable <User> GetAllUsers()
            {
                return _CHGKContainer.Users.OrderBy(m=>m.Login);
            }

            public User GetUser(string login)
            {
                User dbUser;
                {
                    dbUser = _CHGKContainer.Users.SingleOrDefault(t => t.Login == login);
                }
                if (dbUser == null)
                {
                    return null;
                }
                else
                {
                    return dbUser;
                }
            }
            
            public User GetUser(int TeamID)
            {
                 User dbUser;
                {
                    dbUser = _CHGKContainer.Users.SingleOrDefault(t => t.TeamID == TeamID);
                }
                if (dbUser == null)
                {
                    return null;
                }
                else
                {
                    return dbUser;
                }
            }

            public IEnumerable<User> GetUsers(int CompId, string Role)
            {
                return _CHGKContainer.Users.Where(u => u.CompetitionID == CompId && u.Role == Role);
            }

            public string CreateUser(string login, string password, string role, int CompId)
            {
                CryptoHelper CryptoHelperTemp = new CryptoHelper();
                User u = new User();
               
                u.Login = login;
              //  u.HashCode = CryptoHelperTemp.GetSeededHashCode(login, password);
                u.HashCode = password;
                u.Role = role;
                u.CompetitionID = CompId;

                _CHGKContainer.Users.AddObject(u);
                _CHGKContainer.SaveChanges();

                return u.Login;
            }

            /*public string CreateUser(string login, string password, string role)
            {
                CryptoHelper CryptoHelperTemp = new CryptoHelper();
                User u = new User();

                u.Login = login;
                //u.HashCode = CryptoHelperTemp.GetSeededHashCode(login, password);
                u.HashCode = password;
                u.Role = role;

                _CHGKContainer.Users.AddObject(u);
                _CHGKContainer.SaveChanges();

                return u.Login;
            }*/

            public void UpdateUser(User u)
            {
                User dbUser = GetUser(u.Login);
               
                dbUser.HashCode = u.HashCode;
                dbUser.Role = u.Role;
                dbUser.CompetitionID = u.CompetitionID;
               
                _CHGKContainer.SaveChanges();
            }

            public void UpdateUser(string login, bool IsLoggedIn)
            {
                User dbUser = GetUser(login);
                dbUser.IsLoggedIn = IsLoggedIn;

                _CHGKContainer.SaveChanges();
            }

            public void UpdateUser(string login, string Password)
            {
                User dbUser = GetUser(login);
                dbUser.HashCode = Password;

                _CHGKContainer.SaveChanges();
            }

            public void DeleteUser(string login)
            {
                User UserTemp = GetUser(login);
                
                _CHGKContainer.Users.DeleteObject(UserTemp);
                _CHGKContainer.SaveChanges();
            }

            public void DeleteUser(int TeamID)
            {
                User UserTemp = GetUser(TeamID);

                _CHGKContainer.Users.DeleteObject(UserTemp);
                _CHGKContainer.SaveChanges();
            }

            public bool ChangePassword(string login, string OldPassword, string NewPassword)
            {
                User dbUser = GetUser(login);
                CryptoHelper CryptoHelperTemp = new CryptoHelper();
               
                dbUser.HashCode = CryptoHelperTemp.GetSeededHashCode(login, NewPassword);
              
                _CHGKContainer.SaveChanges();
                
                return true;
            }
        /// <summary>
        /// Проверяет, существует ли юзер с введёнными логином и паролем. ВНИМАНИЕ! Сейчас действует версия для наполнения через sql скрипт (проверка без преобразования в хэш-код)
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
            public bool? ValidateUser(string login, string password)
            {
                User dbUser = GetUser(login);
                //CryptoHelper CryptoHelperTemp = new CryptoHelper();
                //string HashCode = CryptoHelperTemp.GetSeededHashCode(login, password);
                
                if (dbUser != null && dbUser.HashCode == password)
                {
                    if (dbUser.IsLoggedIn == true)
                    {
                        return null;
                    }

                    return true;
                }
                
                return false;
            }

            public string GetRoleByLogin(string login)
            {
                User dbUser = GetUser(login);
                
                if (dbUser == null)
                {
                    return null;
                }
              
                return dbUser.Role;
            }

            public void AddTeam(string Login, int TeamID)
            {
                User dbUser = GetUser(Login);
                dbUser.TeamID = TeamID;

                _CHGKContainer.SaveChanges();
            }

            public bool IsRoleExist(string Role)
            {
                if (_CHGKContainer.Users.Where(u => u.Role == Role) != null)
                {
                    return true;
                }

                return false;
            }
    }
}